package main

import (
	"flag"
	log "github.com/micro/go-micro/v2/logger"
	"github.com/micro/go-micro/v2/registry"
	"github.com/micro/go-micro/v2/registry/etcd"
	"github.com/micro/go-micro/v2/web"
	"cirrus-core/config/global_vars"
	"web-service/admin-api/config"
	"web-service/admin-api/config/nacos_client"
	"web-service/admin-api/datasource"
	"web-service/admin-api/middleware"
	"web-service/admin-api/router"
)


func InitServer() {
	//解析命令运行参数
	flag.Parse()
	//从config.json加载配置信息
	log.Info("Config ResourcesPath:", *global_vars.ResourcesPath)
	log.Info("Config Env:", *global_vars.Env)
	//加载环境配置
	config.LoadEnvConfig()
	//从配置中心获取业务配置
	nacos_client.LoadServiceConfig()
	//连接数据库
	datasource.InitDatabase()
}

func main() {
	InitServer()
	conf := config.GetServiceConfig()
	//定义gin router
	ginRouter := router.InitGinRouter()
	//etcd注册中心
	registry:=etcd.NewRegistry(registry.Addrs(conf.RegistryAddr))
	//接收http请求,因此创建一个webService类型的服务,用gin框架做为路由
	httpServer := web.NewService(
		web.Address(":"+conf.Port),
		web.Handler(ginRouter),
		web.Name(conf.ServiceName),
		web.Version(conf.Version),
		web.Registry(registry),
	)

	if err := httpServer.Init(); err != nil {
		log.Fatal(err)
	}

	//创建RPC调用client
	middleware.RegisterRpcClient()

	// Run service
	if err := httpServer.Run(); err != nil {
		log.Fatal(err)
	}
}
